package com.demo.myapplication.activity;

import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.demo.myapplication.R;
import com.demo.myapplication.utils.CommonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ALL on 2017/2/28.
 */
public class UiActivity extends AppCompatActivity {
  private static final String TAG = "UiActivity";
  @BindView(R.id.recycler1)
  RecyclerView recyclerView;

  private final static int DATA_SIZE = 10;
  private SparseBooleanArray data = new SparseBooleanArray(DATA_SIZE);

  private int mScreenWidth;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ui);
    ButterKnife.bind(this);
    initData();

    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerView.setLayoutManager(llm);
    recyclerView.setAdapter(new TwoTypeAdapter());
  }

  private void initData() {
    mScreenWidth = CommonUtils.getScreenWidth();
    for (int i = 0; i < DATA_SIZE; i++) {
      boolean isLarge = (i % 3 == 0);
      data.put(i, isLarge);
    }
  }

  public class TwoTypeAdapter extends RecyclerView.Adapter<TwoTypeAdapter.BaseViewHolder> {
    private final int TYPE_LARGE = 1;
    private final int TYPE_SMALL = 2;

    @Override
    public TwoTypeAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      TwoTypeAdapter.BaseViewHolder viewHolder;
      switch (viewType) {
        case TYPE_LARGE:
          viewHolder = new LargeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_large_item, parent, false));
          break;
        case TYPE_SMALL:
        default:
          viewHolder = new SmallViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_small_item, parent, false));
          break;
      }
      Log.d(TAG, "onCreateViewHolder: viewType=" + viewType + ",viewHolder=" + ((viewHolder instanceof LargeViewHolder) ? "large" : "small"));
      return viewHolder;
    }

    @Override
    public void onBindViewHolder(TwoTypeAdapter.BaseViewHolder holder, int position) {
      int type = getItemViewType(position);
      if (type == TYPE_LARGE) {
        TwoTypeAdapter.LargeViewHolder largeHolder = (LargeViewHolder) holder;
        largeHolder.bigRoot.setLayoutParams(new LinearLayout.LayoutParams(mScreenWidth / 2, LinearLayout.LayoutParams.MATCH_PARENT));
      } else if (type == TYPE_SMALL) {
        TwoTypeAdapter.SmallViewHolder smallHolder = (SmallViewHolder) holder;
        smallHolder.smallRoot.setLayoutParams(new LinearLayout.LayoutParams(mScreenWidth / 2, LinearLayout.LayoutParams.MATCH_PARENT));
      }
    }


    @Override
    public int getItemViewType(int position) {
      boolean isLarge = data.get(position);
      return isLarge ? TYPE_LARGE : TYPE_SMALL;
    }

    @Override
    public int getItemCount() {
      return data.size();
    }

    public class LargeViewHolder extends BaseViewHolder {
      @BindView(R.id.big_root)
      LinearLayout bigRoot;

      public LargeViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
      }
    }

    public class SmallViewHolder extends BaseViewHolder {
      @BindView(R.id.small_root)
      LinearLayout smallRoot;

      public SmallViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
      }
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

      public BaseViewHolder(View itemView) {
        super(itemView);
      }
    }
  }

}
